package com.moonciki.artadmin.core.enums;

import com.moonciki.artadmin.core.constant.GlobalConstant;
import com.moonciki.artadmin.core.model.ResponseCode;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;

@Slf4j
public enum ResponseEnum {
    /** 成功 **/
    success("请求成功"),
    username_error("用户名错误"),
    password_error("密码错误"),
    username_password_not_match("用户名密码不匹配"),

    validate_code_error("验证码错误"),
    login_error("登录失败"),
    user_exist("用户已存在"),
    user_nonexist("用户不存在"),
    request_error("参数错误"),
    sys_error("系统异常"),
    not_found("没有找到资源"),
    no_login("没有登录"),
    no_permission("没有权限"),
    timeout("超时"),
    redis_timeout("系统繁忙"),
    class_type_error("类型错误"),
    sys_busy("系统繁忙"),
    fallback("fallback"),
    illegal_req("非法请求"),

    rest_error("api接口错误"),

    export_error("导出错误"),
    file_template_error("导入模板标题错误或模板标题格式错误"),
    file_template_empty("导入文件内没有数据"),

    ;

    private String dec;

    public String getDec() {
        return dec;
    }

    ResponseEnum(String dec){
        this.dec = dec;
    }

    public ResponseCode info(){
        String codeName = this.name();
        String dec = this.dec;

        ResponseCode respCode = new ResponseCode(codeName, dec);

        return respCode;
    }

    public ResponseCode info(String dec){
        String codeName = this.name();

        ResponseCode respCode = new ResponseCode(codeName, dec);

        return respCode;
    }

    /**
     * 通过code 获取 http 返回码
     * 仅处理特殊返回码
     * @param respCode
     * @return
     */
    public static int getHttpStatus(String respCode){

        int httpStatus = 0;

        if(StringUtils.isNotBlank(respCode)){
            ResponseEnum codeEnum = ResponseEnum.success;

            try {
                codeEnum = ResponseEnum.valueOf(respCode);
            } catch (IllegalArgumentException e) {
                log.info("respCode : " + respCode);
            }

            switch(codeEnum){

                case not_found:
                    httpStatus = GlobalConstant.HttpCode.no_found;
                    break;
                case no_login:
                    httpStatus = GlobalConstant.HttpCode.no_login;
                    break;
                case no_permission:
                    httpStatus = GlobalConstant.HttpCode.no_permission;
                    break;
                default:
                    break;
            }
        }





        int codeInt = GlobalConstant.HttpCode.success;

        if(StringUtils.isNotBlank(respCode)){
            try {
                codeInt = Integer.parseInt(respCode);
            } catch (NumberFormatException e) {
                log.info("respCode : " + respCode);
            }

            switch(codeInt){
                case GlobalConstant.HttpCode.success:
                case GlobalConstant.HttpCode.redirect:
                case GlobalConstant.HttpCode.no_login:
                case GlobalConstant.HttpCode.no_permission:
                case GlobalConstant.HttpCode.no_found:
                case GlobalConstant.HttpCode.sys_error:
                    break;
                default:
                    codeInt = GlobalConstant.HttpCode.success;
                    break;
            }
        }

        return codeInt;
    }

    public static void main(String[] args) {

        String respCode = "no_found1";

        ResponseEnum codeEnum = ResponseEnum.success;

        try {
            codeEnum = ResponseEnum.valueOf(respCode);
        } catch (IllegalArgumentException e) {

        }

        System.out.println(codeEnum);

    }

}
